package com.dense.kuiniu.bookborrow.business.home.query;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.LanguageUtils;
import com.dense.kuiniu.bookborrow.R;
import com.dense.kuiniu.bookborrow.base.BaseFragment;
import com.dense.kuiniu.bookborrow.business.adapter.BookBorrowByUserAdapter;
import com.dense.kuiniu.bookborrow.business.entity.BorrowBookInfo;
import com.dense.kuiniu.bookborrow.business.entity.ReaderInfo;
import com.dense.kuiniu.bookborrow.receiver.SystemTTS;
import com.dense.kuiniu.bookborrow.view.CountDownView;
import com.dense.kuiniu.bookborrow.view.CounterController;
import com.dense.kuiniu.bookborrow.view.SpaceItemDecoration;
import com.dense.kuiniu.bookborrow.xjSetting.config.ShareKeyConfig;
import com.dense.kuiniu.bookborrow.xjSetting.utils.SharePreferceUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rxjava.rxlife.RxLife;

import java.util.ArrayList;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.rxjava3.functions.Consumer;
import rxhttp.RxHttp;

/**
 * @author Administrator
 * @date 2021/1/25
 */
public class QueryBookByUser extends BaseFragment {
    public static QueryBookByUser getInstance(String cardNo, ArrayList<BorrowBookInfo> bookList) {
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("bookList", bookList);
        bundle.putString("cardNo", cardNo);
        QueryBookByUser delegate = new QueryBookByUser();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.count_down_view)
    CountDownView mCountDownView;

    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_card)
    TextView mTvCard;
    @BindView(R.id.tv_can_borrow_count)
    TextView mTvBorrowCount;
    @BindView(R.id.tv_state)
    TextView mTvState;
    private ArrayList<BorrowBookInfo> bookList;
    private String cardNo;
    private volatile boolean hasPop = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            bookList = bundle.getParcelableArrayList("bookList");
            cardNo = bundle.getString("cardNo");
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.delegate_query_book_by_user;
    }

    @Override
    protected void init() {
        Log.e("QueryBookByUser","init");
        queryReaderInfo();
        mTvTitle.setText("查询结果");
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        mRecyclerView.addItemDecoration(new SpaceItemDecoration(20));
        BookBorrowByUserAdapter adapter;
        mRecyclerView.setAdapter(adapter = new BookBorrowByUserAdapter(R.layout.item_book_borrow_by_user, bookList));
        adapter.setEmptyView(R.layout.view_empty);
        int second = SharePreferceUtil.getInstance(getContext()).getInt(ShareKeyConfig.ER_JI_CAIDAN,60);
        mCountDownView.setSeconds(second);
        mCountDownView.start(new CounterController.OnCompleteListener() {
            @Override
            public void onComplete() {
                exit();
            }
        });
        SystemTTS.getInstance().playText("查询完成");
    }

    @OnClick({R.id.tv_return, R.id.tv_complete})
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    protected void onClick2(View view) {
        exit();
    }

    private void exit() {
        if (!hasPop) {
            hasPop = true;
            pop();
        }
    }

    private void queryReaderInfo() {
        RxHttp.get("cslinks/rest/getReaderinforAll")
                .add("ReaderBarcode", cardNo)
                .asString()
                .to(RxLife.toMain(this))
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Throwable {
                        Gson gson = new Gson();
                        ArrayList<ReaderInfo> list = gson.fromJson(s, new TypeToken<ArrayList<ReaderInfo>>() {
                        }.getType());
                        if (list != null && !list.isEmpty()) {
                            setReaderInfo(list.get(0));
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {

                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.e("QueryBookByUser","onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("QueryBookByUser","onDestroy");
    }

    private void setReaderInfo(ReaderInfo info) {
        mTvName.setText(String.format("%s%s", getString(R.string.text_user_name), info.getName()));
        mTvCard.setText(String.format("%s%s", getString(R.string.text_card_no), info.getReaderCode()));
        mTvBorrowCount.setText(String.format(LanguageUtils.getCurrentLocale(),
                "%s%s", "已借数量：",
                info.getBorrowCount()));
        mTvState.setText(String.format("%s%s", getString(R.string.text_state), info.getState()));
    }

}